home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / bfd / elf32-i3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  46.7 KB  |  1,609 lines

  1. /* Intel 80386/80486-specific support for 32-bit ELF
  2.    Copyright 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of BFD, the Binary File Descriptor library.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "bfd.h"
  21. #include "sysdep.h"
  22. #include "bfdlink.h"
  23. #include "libbfd.h"
  24. #include "libelf.h"
  25.  
  26. static CONST struct reloc_howto_struct *elf_i386_reloc_type_lookup
  27.   PARAMS ((bfd *, bfd_reloc_code_real_type));
  28. static void elf_i386_info_to_howto
  29.   PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
  30. static void elf_i386_info_to_howto_rel
  31.   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
  32. static boolean elf_i386_create_dynamic_sections
  33.   PARAMS ((bfd *, struct bfd_link_info *));
  34. static boolean elf_i386_create_got_section
  35.   PARAMS ((bfd *, struct bfd_link_info *));
  36. static boolean elf_i386_check_relocs
  37.   PARAMS ((bfd *, struct bfd_link_info *, asection *,
  38.        const Elf_Internal_Rela *));
  39. static boolean elf_i386_adjust_dynamic_symbol
  40.   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
  41. static boolean elf_i386_size_dynamic_sections
  42.   PARAMS ((bfd *, struct bfd_link_info *));
  43. static boolean elf_i386_relocate_section
  44.   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  45.        Elf_Internal_Rela *, Elf_Internal_Sym *, asection **, char *));
  46. static boolean elf_i386_finish_dynamic_symbol
  47.   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
  48.        Elf_Internal_Sym *));
  49. static boolean elf_i386_finish_dynamic_sections
  50.   PARAMS ((bfd *, struct bfd_link_info *));
  51.  
  52. #define USE_REL    1        /* 386 uses REL relocations instead of RELA */
  53.  
  54. enum reloc_type
  55.   {
  56.     R_386_NONE = 0,
  57.     R_386_32,
  58.     R_386_PC32,
  59.     R_386_GOT32,
  60.     R_386_PLT32,
  61.     R_386_COPY,
  62.     R_386_GLOB_DAT,
  63.     R_386_JUMP_SLOT,
  64.     R_386_RELATIVE,
  65.     R_386_GOTOFF,
  66.     R_386_GOTPC,
  67.     R_386_max
  68.   };
  69.  
  70. #if 0
  71. static CONST char *CONST reloc_type_names[] =
  72. {
  73.   "R_386_NONE",
  74.   "R_386_32",
  75.   "R_386_PC32",
  76.   "R_386_GOT32",
  77.   "R_386_PLT32",
  78.   "R_386_COPY",
  79.   "R_386_GLOB_DAT",
  80.   "R_386_JUMP_SLOT",
  81.   "R_386_RELATIVE",
  82.   "R_386_GOTOFF",
  83.   "R_386_GOTPC",
  84. };
  85. #endif
  86.  
  87. static reloc_howto_type elf_howto_table[]=
  88. {
  89.   HOWTO(R_386_NONE,     0,0, 0,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_NONE",        true,0x00000000,0x00000000,false),
  90.   HOWTO(R_386_32,     0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_32",        true,0xffffffff,0xffffffff,false),
  91.   HOWTO(R_386_PC32,     0,2,32,true, 0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PC32",        true,0xffffffff,0xffffffff,true),
  92.   HOWTO(R_386_GOT32,     0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOT32",    true,0xffffffff,0xffffffff,false),
  93.   HOWTO(R_386_PLT32,     0,2,32,true,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PLT32",    true,0xffffffff,0xffffffff,true),
  94.   HOWTO(R_386_COPY,      0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_COPY",        true,0xffffffff,0xffffffff,false),
  95.   HOWTO(R_386_GLOB_DAT,  0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GLOB_DAT", true,0xffffffff,0xffffffff,false),
  96.   HOWTO(R_386_JUMP_SLOT, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_JUMP_SLOT",true,0xffffffff,0xffffffff,false),
  97.   HOWTO(R_386_RELATIVE,  0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_RELATIVE", true,0xffffffff,0xffffffff,false),
  98.   HOWTO(R_386_GOTOFF,    0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTOFF",   true,0xffffffff,0xffffffff,false),
  99.   HOWTO(R_386_GOTPC,     0,2,32,true,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTPC",    true,0xffffffff,0xffffffff,true),
  100. };
  101.  
  102. #ifdef DEBUG_GEN_RELOC
  103. #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
  104. #else
  105. #define TRACE(str)
  106. #endif
  107.  
  108. static CONST struct reloc_howto_struct *
  109. elf_i386_reloc_type_lookup (abfd, code)
  110.      bfd *abfd;
  111.      bfd_reloc_code_real_type code;
  112. {
  113.   switch (code)
  114.     {
  115.     case BFD_RELOC_NONE:
  116.       TRACE ("BFD_RELOC_NONE");
  117.       return &elf_howto_table[ (int)R_386_NONE ];
  118.  
  119.     case BFD_RELOC_32:
  120.       TRACE ("BFD_RELOC_32");
  121.       return &elf_howto_table[ (int)R_386_32 ];
  122.  
  123.     case BFD_RELOC_32_PCREL:
  124.       TRACE ("BFD_RELOC_PC32");
  125.       return &elf_howto_table[ (int)R_386_PC32 ];
  126.  
  127.     case BFD_RELOC_386_GOT32:
  128.       TRACE ("BFD_RELOC_386_GOT32");
  129.       return &elf_howto_table[ (int)R_386_GOT32 ];
  130.  
  131.     case BFD_RELOC_386_PLT32:
  132.       TRACE ("BFD_RELOC_386_PLT32");
  133.       return &elf_howto_table[ (int)R_386_PLT32 ];
  134.  
  135.     case BFD_RELOC_386_COPY:
  136.       TRACE ("BFD_RELOC_386_COPY");
  137.       return &elf_howto_table[ (int)R_386_COPY ];
  138.  
  139.     case BFD_RELOC_386_GLOB_DAT:
  140.       TRACE ("BFD_RELOC_386_GLOB_DAT");
  141.       return &elf_howto_table[ (int)R_386_GLOB_DAT ];
  142.  
  143.     case BFD_RELOC_386_JUMP_SLOT:
  144.       TRACE ("BFD_RELOC_386_JUMP_SLOT");
  145.       return &elf_howto_table[ (int)R_386_JUMP_SLOT ];
  146.  
  147.     case BFD_RELOC_386_RELATIVE:
  148.       TRACE ("BFD_RELOC_386_RELATIVE");
  149.       return &elf_howto_table[ (int)R_386_RELATIVE ];
  150.  
  151.     case BFD_RELOC_386_GOTOFF:
  152.       TRACE ("BFD_RELOC_386_GOTOFF");
  153.       return &elf_howto_table[ (int)R_386_GOTOFF ];
  154.  
  155.     case BFD_RELOC_386_GOTPC:
  156.       TRACE ("BFD_RELOC_386_GOTPC");
  157.       return &elf_howto_table[ (int)R_386_GOTPC ];
  158.  
  159.     default:
  160.       break;
  161.     }
  162.  
  163.   TRACE ("Unknown");
  164.   return 0;
  165. }
  166.  
  167. static void
  168. elf_i386_info_to_howto (abfd, cache_ptr, dst)
  169.      bfd        *abfd;
  170.      arelent        *cache_ptr;
  171.      Elf32_Internal_Rela *dst;
  172. {
  173.   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max);
  174.  
  175.   cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
  176. }
  177.  
  178. static void
  179. elf_i386_info_to_howto_rel (abfd, cache_ptr, dst)
  180.      bfd        *abfd;
  181.      arelent        *cache_ptr;
  182.      Elf32_Internal_Rel *dst;
  183. {
  184.   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max);
  185.  
  186.   cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
  187. }
  188.  
  189. /* Functions for the i386 ELF linker.  */
  190.  
  191. /* The name of the dynamic interpreter.  This is put in the .interp
  192.    section.  */
  193.  
  194. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
  195.  
  196. /* The size in bytes of an entry in the procedure linkage table.  */
  197.  
  198. #define PLT_ENTRY_SIZE 16
  199.  
  200. /* The first entry in an absolute procedure linkage table looks like
  201.    this.  See the SVR4 ABI i386 supplement to see how this works.  */
  202.  
  203. static bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] =
  204. {
  205.   0xff, 0x35,    /* pushl contents of address */
  206.   0, 0, 0, 0,    /* replaced with address of .got + 4.  */
  207.   0xff, 0x25,    /* jmp indirect */
  208.   0, 0, 0, 0,    /* replaced with address of .got + 8.  */
  209.   0, 0, 0, 0    /* pad out to 16 bytes.  */
  210. };
  211.  
  212. /* Subsequent entries in an absolute procedure linkage table look like
  213.    this.  */
  214.  
  215. static bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
  216. {
  217.   0xff, 0x25,    /* jmp indirect */
  218.   0, 0, 0, 0,    /* replaced with address of this symbol in .got.  */
  219.   0x68,        /* pushl immediate */
  220.   0, 0, 0, 0,    /* replaced with offset into relocation table.  */
  221.   0xe9,        /* jmp relative */
  222.   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
  223. };
  224.  
  225. /* The first entry in a PIC procedure linkage table look like this.  */
  226.  
  227. static bfd_byte elf_i386_pic_plt0_entry[PLT_ENTRY_SIZE] =
  228. {
  229.   0xff, 0xb3, 4, 0, 0, 0,    /* pushl 4(%ebx) */    
  230.   0xff, 0xa3, 8, 0, 0, 0,    /* jmp *8(%ebx) */    
  231.   0, 0, 0, 0            /* pad out to 16 bytes.  */
  232. };
  233.  
  234. /* Subsequent entries in a PIC procedure linkage table look like this.  */
  235.  
  236. static bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
  237. {
  238.   0xff, 0xa3,    /* jmp *offset(%ebx) */
  239.   0, 0, 0, 0,    /* replaced with offset of this symbol in .got.  */
  240.   0x68,        /* pushl immediate */
  241.   0, 0, 0, 0,    /* replaced with offset into relocation table.  */
  242.   0xe9,        /* jmp relative */
  243.   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
  244. };
  245.  
  246. /* Create dynamic sections when linking against a dynamic object.  */
  247.  
  248. static boolean
  249. elf_i386_create_dynamic_sections (abfd, info)
  250.      bfd *abfd;
  251.      struct bfd_link_info *info;
  252. {
  253.   flagword flags;
  254.   register asection *s;
  255.   struct elf_link_hash_entry *h;
  256.  
  257.   /* We need to create .plt, .rel.plt, .got, .got.plt, .dynbss, and
  258.      .rel.bss sections.  */
  259.  
  260.   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
  261.  
  262.   s = bfd_make_section (abfd, ".plt");
  263.   if (s == NULL
  264.       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY | SEC_CODE)
  265.       || ! bfd_set_section_alignment (abfd, s, 2))
  266.     return false;
  267.  
  268.   s = bfd_make_section (abfd, ".rel.plt");
  269.   if (s == NULL
  270.       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
  271.       || ! bfd_set_section_alignment (abfd, s, 2))
  272.     return false;
  273.  
  274.   if (! elf_i386_create_got_section (abfd, info))
  275.     return false;
  276.  
  277.   /* The .dynbss section is a place to put symbols which are defined
  278.      by dynamic objects, are referenced by regular objects, and are
  279.      not functions.  We must allocate space for them in the process
  280.      image and use a R_386_COPY reloc to tell the dynamic linker to
  281.      initialize them at run time.  The linker script puts the .dynbss
  282.      section into the .bss section of the final image.  */
  283.   s = bfd_make_section (abfd, ".dynbss");
  284.   if (s == NULL
  285.       || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
  286.     return false;
  287.  
  288.   /* The .rel.bss section holds copy relocs.  This section is not
  289.      normally needed.  We need to create it here, though, so that the
  290.      linker will map it to an output section.  We can't just create it
  291.      only if we need it, because we will not know whether we need it
  292.      until we have seen all the input files, and the first time the
  293.      main linker code calls BFD after examining all the input files
  294.      (size_dynamic_sections) the input sections have already been
  295.      mapped to the output sections.  If the section turns out not to
  296.      be needed, we can discard it later.  We will never need this
  297.      section when generating a shared object, since they do not use
  298.      copy relocs.  */
  299.   if (! info->shared)
  300.     {
  301.       s = bfd_make_section (abfd, ".rel.bss");
  302.       if (s == NULL
  303.       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
  304.       || ! bfd_set_section_alignment (abfd, s, 2))
  305.     return false;
  306.     }
  307.  
  308.   return true;
  309. }
  310.  
  311. /* Create the .got section to hold the global offset table, and the
  312.    .got.plt section to hold procedure linkage table GOT entries.  The
  313.    linker script will put .got.plt into the output .got section.  */
  314.  
  315. static boolean
  316. elf_i386_create_got_section (abfd, info)
  317.      bfd *abfd;
  318.      struct bfd_link_info *info;
  319. {
  320.   flagword flags;
  321.   register asection *s;
  322.   struct elf_link_hash_entry *h;
  323.  
  324.   /* This function may be called more than once.  */
  325.   if (bfd_get_section_by_name (abfd, ".got") != NULL)
  326.     return true;
  327.  
  328.   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
  329.  
  330.   s = bfd_make_section (abfd, ".got");
  331.   if (s == NULL
  332.       || ! bfd_set_section_flags (abfd, s, flags)
  333.       || ! bfd_set_section_alignment (abfd, s, 2))
  334.     return false;
  335.  
  336.   s = bfd_make_section (abfd, ".got.plt");
  337.   if (s == NULL
  338.       || ! bfd_set_section_flags (abfd, s, flags)
  339.       || ! bfd_set_section_alignment (abfd, s, 2))
  340.     return false;
  341.  
  342.   /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
  343.      .got.plt section, which will be placed at the start of the output
  344.      .got section.  We don't do this in the linker script because we
  345.      don't want to define the symbol if we are not creating a global
  346.      offset table.  */
  347.   h = NULL;
  348.   if (! (_bfd_generic_link_add_one_symbol
  349.      (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0,
  350.       (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
  351.       (struct bfd_link_hash_entry **) &h)))
  352.     return false;
  353.   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  354.   h->type = STT_OBJECT;
  355.  
  356.   if (info->shared
  357.       && ! bfd_elf32_link_record_dynamic_symbol (info, h))
  358.     return false;
  359.  
  360.   /* The first three global offset table entries are reserved.  */
  361.   s->_raw_size += 3 * 4;
  362.  
  363.   return true;
  364. }
  365.  
  366. /* Look through the relocs for a section during the first phase, and
  367.    allocate space in the global offset table or procedure linkage
  368.    table.  */
  369.  
  370. static boolean
  371. elf_i386_check_relocs (abfd, info, sec, relocs)
  372.      bfd *abfd;
  373.      struct bfd_link_info *info;
  374.      asection *sec;
  375.      const Elf_Internal_Rela *relocs;
  376. {
  377.   bfd *dynobj;
  378.   Elf_Internal_Shdr *symtab_hdr;
  379.   struct elf_link_hash_entry **sym_hashes;
  380.   bfd_vma *local_got_offsets;
  381.   const Elf_Internal_Rela *rel;
  382.   const Elf_Internal_Rela *rel_end;
  383.   asection *sgot;
  384.   asection *srelgot;
  385.   asection *sreloc;
  386.  
  387.   if (info->relocateable)
  388.     return true;
  389.  
  390.   dynobj = elf_hash_table (info)->dynobj;
  391.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  392.   sym_hashes = elf_sym_hashes (abfd);
  393.   local_got_offsets = elf_local_got_offsets (abfd);
  394.  
  395.   sgot = NULL;
  396.   srelgot = NULL;
  397.   sreloc = NULL;
  398.  
  399.   rel_end = relocs + sec->reloc_count;
  400.   for (rel = relocs; rel < rel_end; rel++)
  401.     {
  402.       long r_symndx;
  403.       struct elf_link_hash_entry *h;
  404.  
  405.       r_symndx = ELF32_R_SYM (rel->r_info);
  406.  
  407.       if (r_symndx < symtab_hdr->sh_info)
  408.     h = NULL;
  409.       else
  410.     h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  411.  
  412.       /* Some relocs require a global offset table.  */
  413.       if (dynobj == NULL)
  414.     {
  415.       switch (ELF32_R_TYPE (rel->r_info))
  416.         {
  417.         case R_386_GOT32:
  418.         case R_386_GOTOFF:
  419.         case R_386_GOTPC:
  420.           elf_hash_table (info)->dynobj = dynobj = abfd;
  421.           if (! elf_i386_create_got_section (dynobj, info))
  422.         return false;
  423.           break;
  424.  
  425.         default:
  426.           break;
  427.         }
  428.     }
  429.  
  430.       switch (ELF32_R_TYPE (rel->r_info))
  431.     {
  432.     case R_386_GOT32:
  433.       /* This symbol requires a global offset table entry.  */
  434.      
  435.       if (sgot == NULL)
  436.         {
  437.           sgot = bfd_get_section_by_name (dynobj, ".got");
  438.           BFD_ASSERT (sgot != NULL);
  439.         }
  440.  
  441.       if (srelgot == NULL
  442.           && (h != NULL || info->shared))
  443.         {
  444.           srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
  445.           if (srelgot == NULL)
  446.         {
  447.           srelgot = bfd_make_section (dynobj, ".rel.got");
  448.           if (srelgot == NULL
  449.               || ! bfd_set_section_flags (dynobj, srelgot,
  450.                           (SEC_ALLOC
  451.                            | SEC_LOAD
  452.                            | SEC_HAS_CONTENTS
  453.                            | SEC_IN_MEMORY
  454.                            | SEC_READONLY))
  455.               || ! bfd_set_section_alignment (dynobj, srelgot, 2))
  456.             return false;
  457.         }
  458.         }
  459.  
  460.       if (h != NULL)
  461.         {
  462.           if (h->got_offset != (bfd_vma) -1)
  463.         {
  464.           /* We have already allocated space in the .got.  */
  465.           break;
  466.         }
  467.           h->got_offset = sgot->_raw_size;
  468.  
  469.           /* Make sure this symbol is output as a dynamic symbol.  */
  470.           if (h->dynindx == -1)
  471.         {
  472.           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
  473.             return false;
  474.         }
  475.  
  476.           srelgot->_raw_size += sizeof (Elf32_External_Rel);
  477.         }
  478.       else
  479.         {
  480.                /* This is a global offset table entry for a local
  481.                  symbol.  */
  482.           if (local_got_offsets == NULL)
  483.         {
  484.           size_t size;
  485.           register int i;
  486.  
  487.           size = symtab_hdr->sh_info * sizeof (bfd_vma);
  488.           local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
  489.           if (local_got_offsets == NULL)
  490.             {
  491.               bfd_set_error (bfd_error_no_memory);
  492.               return false;
  493.             }
  494.           elf_local_got_offsets (abfd) = local_got_offsets;
  495.           for (i = 0; i < symtab_hdr->sh_info; i++)
  496.             local_got_offsets[i] = (bfd_vma) -1;
  497.         }
  498.           if (local_got_offsets[r_symndx] != (bfd_vma) -1)
  499.         {
  500.           /* We have already allocated space in the .got.  */
  501.           break;
  502.         }
  503.           local_got_offsets[r_symndx] = sgot->_raw_size;
  504.  
  505.           if (info->shared)
  506.         {
  507.           /* If we are generating a shared object, we need to
  508.                      output a R_386_RELATIVE reloc so that the dynamic
  509.                      linker can adjust this GOT entry.  */
  510.           srelgot->_raw_size += sizeof (Elf32_External_Rel);
  511.         }
  512.         }
  513.  
  514.       sgot->_raw_size += 4;
  515.  
  516.       break;
  517.  
  518.     case R_386_PLT32:
  519.       /* This symbol requires a procedure linkage table entry.  We
  520.              actually build the entry in adjust_dynamic_symbol,
  521.              because this might be a case of linking PIC code without
  522.              linking in any dynamic objects, in which case we don't
  523.              need to generate a procedure linkage table after all.  */
  524.       
  525.       /* If this is a local symbol, we resolve it directly without
  526.              creating a procedure linkage table entry.  */
  527.       if (h == NULL)
  528.         continue;
  529.  
  530.       /* Make sure this symbol is output as a dynamic symbol.  */
  531.       if (h->dynindx == -1)
  532.         {
  533.           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
  534.         return false;
  535.         }
  536.  
  537.       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
  538.  
  539.       break;
  540.  
  541.     case R_386_32:
  542.     case R_386_PC32:
  543.       if (info->shared
  544.           && (sec->flags & SEC_ALLOC) != 0)
  545.         {
  546.           /* When creating a shared object, we must copy these
  547.                  reloc types into the output file.  We create a reloc
  548.                  section in dynobj and make room for this reloc.  */
  549.           if (sreloc == NULL)
  550.         {
  551.           const char *name;
  552.  
  553.           name = (elf_string_from_elf_section
  554.               (abfd,
  555.                elf_elfheader (abfd)->e_shstrndx,
  556.                elf_section_data (sec)->rel_hdr.sh_name));
  557.           if (name == NULL)
  558.             return false;
  559.  
  560.           BFD_ASSERT (strncmp (name, ".rel", 4) == 0
  561.                   && strcmp (bfd_get_section_name (abfd, sec),
  562.                      name + 4) == 0);
  563.  
  564.           sreloc = bfd_get_section_by_name (dynobj, name);
  565.           if (sreloc == NULL)
  566.             {
  567.               sreloc = bfd_make_section (dynobj, name);
  568.               if (sreloc == NULL
  569.               || ! bfd_set_section_flags (dynobj, sreloc,
  570.                               (SEC_ALLOC
  571.                                | SEC_LOAD
  572.                                | SEC_HAS_CONTENTS
  573.                                | SEC_IN_MEMORY
  574.                                | SEC_READONLY))
  575.               || ! bfd_set_section_alignment (dynobj, sreloc, 2))
  576.             return false;
  577.             }
  578.         }
  579.  
  580.           sreloc->_raw_size += sizeof (Elf32_External_Rel);
  581.         }
  582.          
  583.       break;
  584.  
  585.     default:
  586.       break;
  587.     }
  588.     }
  589.  
  590.   return true;
  591. }
  592.  
  593. /* Adjust a symbol defined by a dynamic object and referenced by a
  594.    regular object.  The current definition is in some section of the
  595.    dynamic object, but we're not including those sections.  We have to
  596.    change the definition to something the rest of the link can
  597.    understand.  */
  598.  
  599. static boolean
  600. elf_i386_adjust_dynamic_symbol (info, h)
  601.      struct bfd_link_info *info;
  602.      struct elf_link_hash_entry *h;
  603. {
  604.   bfd *dynobj;
  605.   asection *s;
  606.   unsigned int power_of_two;
  607.  
  608.   dynobj = elf_hash_table (info)->dynobj;
  609.  
  610.   /* Make sure we know what is going on here.  */
  611.   BFD_ASSERT (dynobj != NULL
  612.           && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
  613.           || ((h->elf_link_hash_flags
  614.                & ELF_LINK_HASH_DEF_DYNAMIC) != 0
  615.               && (h->elf_link_hash_flags
  616.               & ELF_LINK_HASH_REF_REGULAR) != 0
  617.               && (h->elf_link_hash_flags
  618.               & ELF_LINK_HASH_DEF_REGULAR) == 0
  619.               && (elf_elfheader (h->root.u.def.section->owner)->e_type
  620.               == ET_DYN)
  621.               && h->root.type == bfd_link_hash_defined
  622.               && (bfd_get_flavour (h->root.u.def.section->owner)
  623.               == bfd_target_elf_flavour)
  624.               && h->root.u.def.section->output_section == NULL)));
  625.  
  626.   /* If this is a function, put it in the procedure linkage table.  We
  627.      will fill in the contents of the procedure linkage table later,
  628.      when we know the address of the .got section.  */
  629.   if (h->type == STT_FUNC
  630.       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
  631.     {
  632.       if (! elf_hash_table (info)->dynamic_sections_created)
  633.     {
  634.       /* This case can occur if we saw a PLT32 reloc in an input
  635.              file, but none of the input files were dynamic objects.
  636.              In such a case, we don't actually need to build a
  637.              procedure linkage table, and we can just do a PC32 reloc
  638.              instead.  */
  639.       BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
  640.       return true;
  641.     }
  642.  
  643.       s = bfd_get_section_by_name (dynobj, ".plt");
  644.       BFD_ASSERT (s != NULL);
  645.  
  646.       /* If this is the first .plt entry, make room for the special
  647.      first entry.  */
  648.       if (s->_raw_size == 0)
  649.     s->_raw_size += PLT_ENTRY_SIZE;
  650.  
  651.       /* If this symbol is not defined in a regular file, and we are
  652.      not generating a shared library, then set the symbol to this
  653.      location in the .plt.  This is required to make function
  654.      pointers compare as equal between the normal executable and
  655.      the shared library.  */
  656.       if (! info->shared
  657.       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
  658.     {
  659.       h->root.u.def.section = s;
  660.       h->root.u.def.value = s->_raw_size;
  661.     }
  662.  
  663.       h->plt_offset = s->_raw_size;
  664.  
  665.       /* Make room for this entry.  */
  666.       s->_raw_size += PLT_ENTRY_SIZE;
  667.  
  668.       /* We also need to make an entry in the .got.plt section, which
  669.      will be placed in the .got section by the linker script.  */
  670.  
  671.       s = bfd_get_section_by_name (dynobj, ".got.plt");
  672.       BFD_ASSERT (s != NULL);
  673.       s->_raw_size += 4;
  674.  
  675.       /* We also need to make an entry in the .rel.plt section.  */
  676.  
  677.       s = bfd_get_section_by_name (dynobj, ".rel.plt");
  678.       BFD_ASSERT (s != NULL);
  679.       s->_raw_size += sizeof (Elf32_External_Rel);
  680.  
  681.       return true;
  682.     }
  683.  
  684.   /* If this is a weak symbol, and there is a real definition, the
  685.      processor independent code will have arranged for us to see the
  686.      real definition first, and we can just use the same value.  */
  687.   if (h->weakdef != NULL)
  688.     {
  689.       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined);
  690.       h->root.u.def.section = h->weakdef->root.u.def.section;
  691.       h->root.u.def.value = h->weakdef->root.u.def.value;
  692.       return true;
  693.     }
  694.  
  695.   /* This is a reference to a symbol defined by a dynamic object which
  696.      is not a function.  */
  697.  
  698.   /* If we are creating a shared library, we must presume that the
  699.      only references to the symbol are via the global offset table.
  700.      For such cases we need not do anything here; the relocations will
  701.      be handled correctly by relocate_section.  */
  702.   if (info->shared)
  703.     return true;
  704.  
  705.   /* We must allocate the symbol in our .dynbss section, which will
  706.      become part of the .bss section of the executable.  There will be
  707.      an entry for this symbol in the .dynsym section.  The dynamic
  708.      object will contain position independent code, so all references
  709.      from the dynamic object to this symbol will go through the global
  710.      offset table.  The dynamic linker will use the .dynsym entry to
  711.      determine the address it must put in the global offset table, so
  712.      both the dynamic object and the regular object will refer to the
  713.      same memory location for the variable.  */
  714.  
  715.   s = bfd_get_section_by_name (dynobj, ".dynbss");
  716.   BFD_ASSERT (s != NULL);
  717.  
  718.   /* If the symbol is currently defined in the .bss section of the
  719.      dynamic object, then it is OK to simply initialize it to zero.
  720.      If the symbol is in some other section, we must generate a
  721.      R_386_COPY reloc to tell the dynamic linker to copy the initial
  722.      value out of the dynamic object and into the runtime process
  723.      image.  We need to remember the offset into the .rel.bss section
  724.      we are going to use.  */
  725.   if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
  726.     {
  727.       asection *srel;
  728.  
  729.       srel = bfd_get_section_by_name (dynobj, ".rel.bss");
  730.       BFD_ASSERT (srel != NULL);
  731.       srel->_raw_size += sizeof (Elf32_External_Rel);
  732.       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
  733.     }
  734.  
  735.   /* We need to figure out the alignment required for this symbol.  I
  736.      have no idea how ELF linkers handle this.  */
  737.   power_of_two = bfd_log2 (h->size);
  738.   if (power_of_two > 3)
  739.     power_of_two = 3;
  740.  
  741.   /* Apply the required alignment.  */
  742.   s->_raw_size = BFD_ALIGN (s->_raw_size,
  743.                 (bfd_size_type) (1 << power_of_two));
  744.   if (power_of_two > bfd_get_section_alignment (dynobj, s))
  745.     {
  746.       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
  747.     return false;
  748.     }
  749.  
  750.   /* Define the symbol as being at this point in the section.  */
  751.   h->root.u.def.section = s;
  752.   h->root.u.def.value = s->_raw_size;
  753.  
  754.   /* Increment the section size to make room for the symbol.  */
  755.   s->_raw_size += h->size;
  756.  
  757.   return true;
  758. }
  759.  
  760. /* Set the sizes of the dynamic sections.  */
  761.  
  762. static boolean
  763. elf_i386_size_dynamic_sections (output_bfd, info)
  764.      bfd *output_bfd;
  765.      struct bfd_link_info *info;
  766. {
  767.   bfd *dynobj;
  768.   asection *s;
  769.   boolean plt;
  770.   boolean relocs;
  771.   boolean reltext;
  772.  
  773.   dynobj = elf_hash_table (info)->dynobj;
  774.   BFD_ASSERT (dynobj != NULL);
  775.  
  776.   if (elf_hash_table (info)->dynamic_sections_created)
  777.     {
  778.       /* Set the contents of the .interp section to the interpreter.  */
  779.       if (! info->shared)
  780.     {
  781.       s = bfd_get_section_by_name (dynobj, ".interp");
  782.       BFD_ASSERT (s != NULL);
  783.       s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
  784.       s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  785.     }
  786.     }
  787.   else
  788.     {
  789.       /* We may have created entries in the .rel.got section.
  790.          However, if we are not creating the dynamic sections, we will
  791.          not actually use these entries.  Reset the size of .rel.got,
  792.          which will cause it to get stripped from the output file
  793.          below.  */
  794.       s = bfd_get_section_by_name (dynobj, ".rel.got");
  795.       if (s != NULL)
  796.     s->_raw_size = 0;
  797.     }
  798.  
  799.   /* The check_relocs and adjust_dynamic_symbol entry points have
  800.      determined the sizes of the various dynamic sections.  Allocate
  801.      memory for them.  */
  802.   plt = false;
  803.   relocs = false;
  804.   reltext = false;
  805.   for (s = dynobj->sections; s != NULL; s = s->next)
  806.     {
  807.       const char *name;
  808.       boolean strip;
  809.  
  810.       if ((s->flags & SEC_IN_MEMORY) == 0)
  811.     continue;
  812.  
  813.       /* It's OK to base decisions on the section name, because none
  814.      of the dynobj section names depend upon the input files.  */
  815.       name = bfd_get_section_name (dynobj, s);
  816.  
  817.       strip = false;
  818.  
  819.       if (strcmp (name, ".plt") == 0)
  820.     {
  821.       if (s->_raw_size == 0)
  822.         {
  823.           /* Strip this section if we don't need it; see the
  824.                  comment below.  */
  825.           strip = true;
  826.         }
  827.       else
  828.         {
  829.           /* Remember whether there is a PLT.  */
  830.           plt = true;
  831.         }
  832.     }
  833.       else if (strncmp (name, ".rel", 4) == 0)
  834.     {
  835.       if (s->_raw_size == 0)
  836.         {
  837.           /* If we don't need this section, strip it from the
  838.          output file.  This is mostly to handle .rel.bss and
  839.          .rel.plt.  We must create both sections in
  840.          create_dynamic_sections, because they must be created
  841.          before the linker maps input sections to output
  842.          sections.  The linker does that before
  843.          adjust_dynamic_symbol is called, and it is that
  844.          function which decides whether anything needs to go
  845.          into these sections.  */
  846.           strip = true;
  847.         }
  848.       else
  849.         {
  850.           asection *target;
  851.  
  852.           /* Remember whether there are any reloc sections other
  853.                  than .rel.plt.  */
  854.           if (strcmp (name, ".rel.plt") != 0)
  855.         relocs = true;
  856.  
  857.           /* If this relocation section applies to a read only
  858.                  section, then we probably need a DT_TEXTREL entry.  */
  859.           target = bfd_get_section_by_name (output_bfd, name + 4);
  860.           if (target != NULL
  861.           && (target->flags & SEC_READONLY) != 0)
  862.         reltext = true;
  863.  
  864.           /* We use the reloc_count field as a counter if we need
  865.          to copy relocs into the output file.  */
  866.           s->reloc_count = 0;
  867.         }
  868.     }
  869.       else if (strncmp (name, ".got", 4) != 0)
  870.     {
  871.       /* It's not one of our sections, so don't allocate space.  */
  872.       continue;
  873.     }
  874.  
  875.       if (strip)
  876.     {
  877.       asection **spp;
  878.  
  879.       for (spp = &s->output_section->owner->sections;
  880.            *spp != s->output_section;
  881.            spp = &(*spp)->next)
  882.         ;
  883.       *spp = s->output_section->next;
  884.       --s->output_section->owner->section_count;
  885.  
  886.       continue;
  887.     }
  888.  
  889.       /* Allocate memory for the section contents.  */
  890.       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
  891.       if (s->contents == NULL && s->_raw_size != 0)
  892.     {
  893.       bfd_set_error (bfd_error_no_memory);
  894.       return false;
  895.     }
  896.     }
  897.       
  898.   if (elf_hash_table (info)->dynamic_sections_created)
  899.     {
  900.       /* Add some entries to the .dynamic section.  We fill in the
  901.      values later, in elf_i386_finish_dynamic_sections, but we
  902.      must add the entries now so that we get the correct size for
  903.      the .dynamic section.  The DT_DEBUG entry is filled in by the
  904.      dynamic linker and used by the debugger.  */
  905.       if (! info->shared)
  906.     {
  907.       if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
  908.         return false;
  909.     }
  910.  
  911.       if (plt)
  912.     {
  913.       if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
  914.           || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
  915.           || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_REL)
  916.           || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
  917.         return false;
  918.     }
  919.  
  920.       if (relocs)
  921.     {
  922.       if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0)
  923.           || ! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0)
  924.           || ! bfd_elf32_add_dynamic_entry (info, DT_RELENT,
  925.                         sizeof (Elf32_External_Rel)))
  926.         return false;
  927.     }
  928.  
  929.       if (reltext)
  930.     {
  931.       if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
  932.         return false;
  933.     }
  934.     }
  935.  
  936.   return true;
  937. }
  938.  
  939. /* Relocate an i386 ELF section.  */
  940.  
  941. static boolean
  942. elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
  943.                contents, relocs, local_syms, local_sections,
  944.                output_names)
  945.      bfd *output_bfd;
  946.      struct bfd_link_info *info;
  947.      bfd *input_bfd;
  948.      asection *input_section;
  949.      bfd_byte *contents;
  950.      Elf_Internal_Rela *relocs;
  951.      Elf_Internal_Sym *local_syms;
  952.      asection **local_sections;
  953.      char *output_names;
  954. {
  955.   bfd *dynobj;
  956.   Elf_Internal_Shdr *symtab_hdr;
  957.   struct elf_link_hash_entry **sym_hashes;
  958.   bfd_vma *local_got_offsets;
  959.   asection *sgot;
  960.   asection *splt;
  961.   asection *sreloc;
  962.   Elf_Internal_Rela *rel;
  963.   Elf_Internal_Rela *relend;
  964.  
  965.   dynobj = elf_hash_table (info)->dynobj;
  966.   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  967.   sym_hashes = elf_sym_hashes (input_bfd);
  968.   local_got_offsets = elf_local_got_offsets (input_bfd);
  969.  
  970.   sgot = NULL;
  971.   splt = NULL;
  972.   sreloc = NULL;
  973.  
  974.   rel = relocs;
  975.   relend = relocs + input_section->reloc_count;
  976.   for (; rel < relend; rel++)
  977.     {
  978.       int r_type;
  979.       const reloc_howto_type *howto;
  980.       long r_symndx;
  981.       struct elf_link_hash_entry *h;
  982.       Elf_Internal_Sym *sym;
  983.       asection *sec;
  984.       bfd_vma relocation;
  985.       bfd_reloc_status_type r;
  986.  
  987.       r_type = ELF32_R_TYPE (rel->r_info);
  988.       if (r_type < 0 || r_type >= (int) R_386_max)
  989.     {
  990.       bfd_set_error (bfd_error_bad_value);
  991.       return false;
  992.     }
  993.       howto = elf_howto_table + r_type;
  994.  
  995.       r_symndx = ELF32_R_SYM (rel->r_info);
  996.  
  997.       if (info->relocateable)
  998.     {
  999.       /* This is a relocateable link.  We don't have to change
  1000.          anything, unless the reloc is against a section symbol,
  1001.          in which case we have to adjust according to where the
  1002.          section symbol winds up in the output section.  */
  1003.       if (r_symndx < symtab_hdr->sh_info)
  1004.         {
  1005.           sym = local_syms + r_symndx;
  1006.           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  1007.         {
  1008.           bfd_vma val;
  1009.  
  1010.           sec = local_sections[r_symndx];
  1011.           val = bfd_get_32 (input_bfd, contents + rel->r_offset);
  1012.           val += sec->output_offset + sym->st_value;
  1013.           bfd_put_32 (input_bfd, val, contents + rel->r_offset);
  1014.         }
  1015.         }
  1016.  
  1017.       continue;
  1018.     }
  1019.  
  1020.       /* This is a final link.  */
  1021.       h = NULL;
  1022.       sym = NULL;
  1023.       sec = NULL;
  1024.       if (r_symndx < symtab_hdr->sh_info)
  1025.     {
  1026.       sym = local_syms + r_symndx;
  1027.       sec = local_sections[r_symndx];
  1028.       relocation = (sec->output_section->vma
  1029.             + sec->output_offset
  1030.             + sym->st_value);
  1031.     }
  1032.       else
  1033.     {
  1034.       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1035.       if (h->root.type == bfd_link_hash_defined)
  1036.         {
  1037.           sec = h->root.u.def.section;
  1038.           relocation = (h->root.u.def.value
  1039.                 + sec->output_section->vma
  1040.                 + sec->output_offset);
  1041.         }
  1042.       else if (h->root.type == bfd_link_hash_weak)
  1043.         relocation = 0;
  1044.       else if (info->shared)
  1045.         relocation = 0;
  1046.       else
  1047.         {
  1048.           if (! ((*info->callbacks->undefined_symbol)
  1049.              (info, h->root.root.string, input_bfd,
  1050.               input_section, rel->r_offset)))
  1051.         return false;
  1052.           relocation = 0;
  1053.         }
  1054.     }
  1055.  
  1056.       switch (r_type)
  1057.     {
  1058.     case R_386_GOT32:
  1059.       /* Relocation is to the entry for this symbol in the global
  1060.          offset table.  */
  1061.       if (sgot == NULL)
  1062.         {
  1063.           sgot = bfd_get_section_by_name (dynobj, ".got");
  1064.           BFD_ASSERT (sgot != NULL);
  1065.         }
  1066.  
  1067.       if (h != NULL)
  1068.         {
  1069.           bfd_vma off;
  1070.  
  1071.           off = h->got_offset;
  1072.           BFD_ASSERT (off != (bfd_vma) -1);
  1073.  
  1074.           if (! elf_hash_table (info)->dynamic_sections_created)
  1075.         {
  1076.           /* This is actually a static link.  We must
  1077.              initialize this entry in the global offset table.
  1078.              Since the offset must always be a multiple of 4,
  1079.              we use the least significant bit to record
  1080.              whether we have initialized it already.
  1081.  
  1082.              When doing a dynamic link, we create a .rel.got
  1083.              relocation entry to initialize the value.  This
  1084.              is done in the finish_dynamic_symbol routine.  */
  1085.           if ((off & 1) != 0)
  1086.             off &= ~1;
  1087.           else
  1088.             {
  1089.               bfd_put_32 (output_bfd, relocation,
  1090.                   sgot->contents + off);
  1091.               h->got_offset |= 1;
  1092.             }
  1093.         }
  1094.  
  1095.           relocation = sgot->output_offset + off;
  1096.         }
  1097.       else
  1098.         {
  1099.           bfd_vma off;
  1100.  
  1101.           BFD_ASSERT (local_got_offsets != NULL
  1102.               && local_got_offsets[r_symndx] != (bfd_vma) -1);
  1103.  
  1104.           off = local_got_offsets[r_symndx];
  1105.  
  1106.           /* The offset must always be a multiple of 4.  We use
  1107.                  the least significant bit to record whether we have
  1108.                  already generated the necessary reloc.  */
  1109.           if ((off & 1) != 0)
  1110.         off &= ~1;
  1111.           else
  1112.         {
  1113.           bfd_put_32 (output_bfd, relocation, sgot->contents + off);
  1114.  
  1115.           if (info->shared)
  1116.             {
  1117.               asection *srelgot;
  1118.               Elf_Internal_Rel outrel;
  1119.  
  1120.               srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
  1121.               BFD_ASSERT (srelgot != NULL);
  1122.  
  1123.               outrel.r_offset = (sgot->output_section->vma
  1124.                      + sgot->output_offset
  1125.                      + off);
  1126.               outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
  1127.               bfd_elf32_swap_reloc_out (output_bfd, &outrel,
  1128.                         (((Elf32_External_Rel *)
  1129.                           srelgot->contents)
  1130.                          + srelgot->reloc_count));
  1131.               ++srelgot->reloc_count;
  1132.             }
  1133.  
  1134.           local_got_offsets[r_symndx] |= 1;
  1135.         }
  1136.  
  1137.           relocation = sgot->output_offset + off;
  1138.         }
  1139.  
  1140.       break;
  1141.  
  1142.     case R_386_GOTOFF:
  1143.       /* Relocation is relative to the start of the global offset
  1144.          table.  */
  1145.  
  1146.       if (sgot == NULL)
  1147.         {
  1148.           sgot = bfd_get_section_by_name (dynobj, ".got");
  1149.           BFD_ASSERT (sgot != NULL);
  1150.         }
  1151.  
  1152.       /* Note that sgot->output_offset is not involved in this
  1153.          calculation.  We always want the start of .got.  If we
  1154.          defined _GLOBAL_OFFSET_TABLE in a different way, as is
  1155.          permitted by the ABI, we might have to change this
  1156.          calculation.  */
  1157.       relocation -= sgot->output_section->vma;
  1158.  
  1159.       break;
  1160.  
  1161.     case R_386_GOTPC:
  1162.       /* Use global offset table as symbol value.  */
  1163.  
  1164.       if (sgot == NULL)
  1165.         {
  1166.           sgot = bfd_get_section_by_name (dynobj, ".got");
  1167.           BFD_ASSERT (sgot != NULL);
  1168.         }
  1169.  
  1170.       relocation = sgot->output_section->vma;
  1171.  
  1172.       break;
  1173.  
  1174.     case R_386_PLT32:
  1175.       /* Relocation is to the entry for this symbol in the
  1176.          procedure linkage table.  */
  1177.  
  1178.       /* Resolve a PLT32 reloc again a local symbol directly,
  1179.              without using the procedure linkage table.  */
  1180.       if (h == NULL)
  1181.         break;
  1182.  
  1183.       if (h->plt_offset == (bfd_vma) -1)
  1184.         {
  1185.           /* We didn't make a PLT entry for this symbol.  This
  1186.                  happens when statically linking PIC code.  */
  1187.           break;
  1188.         }
  1189.  
  1190.       if (splt == NULL)
  1191.         {
  1192.           splt = bfd_get_section_by_name (dynobj, ".plt");
  1193.           BFD_ASSERT (splt != NULL);
  1194.         }
  1195.  
  1196.       relocation = (splt->output_section->vma
  1197.             + splt->output_offset
  1198.             + h->plt_offset);
  1199.  
  1200.       break;
  1201.  
  1202.     case R_386_32:
  1203.     case R_386_PC32:
  1204.       if (info->shared
  1205.           && (input_section->flags & SEC_ALLOC) != 0)
  1206.         {
  1207.           Elf_Internal_Rel outrel;
  1208.  
  1209.           /* When generating a shared object, these relocations
  1210.          are copied into the output file to be resolved at run
  1211.          time.  */
  1212.  
  1213.           if (sreloc == NULL)
  1214.         {
  1215.           const char *name;
  1216.  
  1217.           name = (elf_string_from_elf_section
  1218.               (input_bfd,
  1219.                elf_elfheader (input_bfd)->e_shstrndx,
  1220.                elf_section_data (input_section)->rel_hdr.sh_name));
  1221.           if (name == NULL)
  1222.             return false;
  1223.  
  1224.           BFD_ASSERT (strncmp (name, ".rel", 4) == 0
  1225.                   && strcmp (bfd_get_section_name (input_bfd,
  1226.                                    input_section),
  1227.                      name + 4) == 0);
  1228.  
  1229.           sreloc = bfd_get_section_by_name (dynobj, name);
  1230.           BFD_ASSERT (sreloc != NULL);
  1231.         }
  1232.  
  1233.           outrel.r_offset = (rel->r_offset
  1234.                  + input_section->output_section->vma
  1235.                  + input_section->output_offset);
  1236.           if (r_type == R_386_PC32)
  1237.         {
  1238.           BFD_ASSERT (h != NULL && h->dynindx != -1);
  1239.           outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_PC32);
  1240.         }
  1241.           else
  1242.         {
  1243.           if (h == NULL)
  1244.             outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
  1245.           else
  1246.             {
  1247.               BFD_ASSERT (h->dynindx != (bfd_vma) -1);
  1248.               outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_32);
  1249.             }
  1250.         }
  1251.  
  1252.           bfd_elf32_swap_reloc_out (output_bfd, &outrel,
  1253.                     (((Elf32_External_Rel *)
  1254.                       sreloc->contents)
  1255.                      + sreloc->reloc_count));
  1256.           ++sreloc->reloc_count;
  1257.  
  1258.           /* If this reloc is against an external symbol, we do
  1259.          not want to fiddle with the addend.  Otherwise, we
  1260.          need to include the symbol value so that it becomes
  1261.          an addend for the dynamic reloc.  */
  1262.           if (h != NULL)
  1263.         continue;
  1264.         }
  1265.  
  1266.       break;
  1267.  
  1268.     default:
  1269.       break;
  1270.     }
  1271.  
  1272.       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  1273.                     contents, rel->r_offset,
  1274.                     relocation, (bfd_vma) 0);
  1275.  
  1276.       if (r != bfd_reloc_ok)
  1277.     {
  1278.       switch (r)
  1279.         {
  1280.         default:
  1281.         case bfd_reloc_outofrange:
  1282.           abort ();
  1283.         case bfd_reloc_overflow:
  1284.           {
  1285.         const char *name;
  1286.  
  1287.         if (h != NULL)
  1288.           name = h->root.root.string;
  1289.         else
  1290.           {
  1291.             name = output_names + sym->st_name;
  1292.             if (name == NULL)
  1293.               return false;
  1294.             if (*name == '\0')
  1295.               name = bfd_section_name (input_bfd, sec);
  1296.           }
  1297.         if (! ((*info->callbacks->reloc_overflow)
  1298.                (info, name, howto->name, (bfd_vma) 0,
  1299.             input_bfd, input_section, rel->r_offset)))
  1300.           return false;
  1301.           }
  1302.           break;
  1303.         }
  1304.     }
  1305.     }
  1306.  
  1307.   return true;
  1308. }
  1309.  
  1310. /* Finish up dynamic symbol handling.  We set the contents of various
  1311.    dynamic sections here.  */
  1312.  
  1313. static boolean
  1314. elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym)
  1315.      bfd *output_bfd;
  1316.      struct bfd_link_info *info;
  1317.      struct elf_link_hash_entry *h;
  1318.      Elf_Internal_Sym *sym;
  1319. {
  1320.   bfd *dynobj;
  1321.  
  1322.   dynobj = elf_hash_table (info)->dynobj;
  1323.  
  1324.   if (h->plt_offset != (bfd_vma) -1)
  1325.     {
  1326.       asection *splt;
  1327.       asection *sgot;
  1328.       asection *srel;
  1329.       bfd_vma plt_index;
  1330.       bfd_vma got_offset;
  1331.       Elf_Internal_Rel rel;
  1332.  
  1333.       /* This symbol has an entry in the procedure linkage table.  Set
  1334.      it up.  */
  1335.  
  1336.       BFD_ASSERT (h->dynindx != -1);
  1337.  
  1338.       splt = bfd_get_section_by_name (dynobj, ".plt");
  1339.       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
  1340.       srel = bfd_get_section_by_name (dynobj, ".rel.plt");
  1341.       BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
  1342.  
  1343.       /* Get the index in the procedure linkage table which
  1344.      corresponds to this symbol.  This is the index of this symbol
  1345.      in all the symbols for which we are making plt entries.  The
  1346.      first entry in the procedure linkage table is reserved.  */
  1347.       plt_index = h->plt_offset / PLT_ENTRY_SIZE - 1;
  1348.  
  1349.       /* Get the offset into the .got table of the entry that
  1350.      corresponds to this function.  Each .got entry is 4 bytes.
  1351.      The first three are reserved.  */
  1352.       got_offset = (plt_index + 3) * 4;
  1353.  
  1354.       /* Fill in the entry in the procedure linkage table.  */
  1355.       if (! info->shared)
  1356.     {
  1357.       memcpy (splt->contents + h->plt_offset, elf_i386_plt_entry,
  1358.           PLT_ENTRY_SIZE);
  1359.       bfd_put_32 (output_bfd,
  1360.               (sgot->output_section->vma
  1361.                + sgot->output_offset
  1362.                + got_offset),
  1363.               splt->contents + h->plt_offset + 2);
  1364.     }
  1365.       else
  1366.     {
  1367.       memcpy (splt->contents + h->plt_offset, elf_i386_pic_plt_entry,
  1368.           PLT_ENTRY_SIZE);
  1369.       bfd_put_32 (output_bfd, got_offset,
  1370.               splt->contents + h->plt_offset + 2);
  1371.     }
  1372.  
  1373.       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
  1374.           splt->contents + h->plt_offset + 7);
  1375.       bfd_put_32 (output_bfd, - (h->plt_offset + PLT_ENTRY_SIZE),
  1376.           splt->contents + h->plt_offset + 12);
  1377.  
  1378.       /* Fill in the entry in the global offset table.  */
  1379.       bfd_put_32 (output_bfd,
  1380.           (splt->output_section->vma
  1381.            + splt->output_offset
  1382.            + h->plt_offset
  1383.            + 6),
  1384.           sgot->contents + got_offset);
  1385.  
  1386.       /* Fill in the entry in the .rel.plt section.  */
  1387.       rel.r_offset = (sgot->output_section->vma
  1388.               + sgot->output_offset
  1389.               + got_offset);
  1390.       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
  1391.       bfd_elf32_swap_reloc_out (output_bfd, &rel,
  1392.                 ((Elf32_External_Rel *) srel->contents
  1393.                  + plt_index));
  1394.  
  1395.       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
  1396.     {
  1397.       /* Mark the symbol as undefined, rather than as defined in
  1398.          the .plt section.  Leave the value alone.  */
  1399.       sym->st_shndx = SHN_UNDEF;
  1400.     }
  1401.     }
  1402.  
  1403.   if (h->got_offset != (bfd_vma) -1)
  1404.     {
  1405.       asection *sgot;
  1406.       asection *srel;
  1407.       Elf_Internal_Rel rel;
  1408.  
  1409.       /* This symbol has an entry in the global offset table.  Set it
  1410.      up.  */
  1411.       
  1412.       BFD_ASSERT (h->dynindx != -1);
  1413.  
  1414.       sgot = bfd_get_section_by_name (dynobj, ".got");
  1415.       srel = bfd_get_section_by_name (dynobj, ".rel.got");
  1416.       BFD_ASSERT (sgot != NULL && srel != NULL);
  1417.  
  1418.       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset);
  1419.  
  1420.       rel.r_offset = (sgot->output_section->vma
  1421.               + sgot->output_offset
  1422.               + h->got_offset);
  1423.       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
  1424.       bfd_elf32_swap_reloc_out (output_bfd, &rel,
  1425.                 ((Elf32_External_Rel *) srel->contents
  1426.                  + srel->reloc_count));
  1427.       ++srel->reloc_count;
  1428.     }
  1429.  
  1430.   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
  1431.     {
  1432.       asection *s;
  1433.       Elf_Internal_Rel rel;
  1434.  
  1435.       /* This symbol needs a copy reloc.  Set it up.  */
  1436.  
  1437.       BFD_ASSERT (h->dynindx != -1
  1438.           && h->root.type == bfd_link_hash_defined);
  1439.  
  1440.       s = bfd_get_section_by_name (h->root.u.def.section->owner,
  1441.                    ".rel.bss");
  1442.       BFD_ASSERT (s != NULL);
  1443.  
  1444.       rel.r_offset = (h->root.u.def.value
  1445.               + h->root.u.def.section->output_section->vma
  1446.               + h->root.u.def.section->output_offset);
  1447.       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
  1448.       bfd_elf32_swap_reloc_out (output_bfd, &rel,
  1449.                 ((Elf32_External_Rel *) s->contents
  1450.                  + s->reloc_count));
  1451.       ++s->reloc_count;
  1452.     }
  1453.  
  1454.   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
  1455.   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
  1456.       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
  1457.     sym->st_shndx = SHN_ABS;
  1458.  
  1459.   return true;
  1460. }
  1461.  
  1462. /* Finish up the dynamic sections.  */
  1463.  
  1464. static boolean
  1465. elf_i386_finish_dynamic_sections (output_bfd, info)
  1466.      bfd *output_bfd;
  1467.      struct bfd_link_info *info;
  1468. {
  1469.   bfd *dynobj;
  1470.   asection *sgot;
  1471.   asection *sdyn;
  1472.  
  1473.   dynobj = elf_hash_table (info)->dynobj;
  1474.  
  1475.   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
  1476.   BFD_ASSERT (sgot != NULL);
  1477.   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
  1478.  
  1479.   if (elf_hash_table (info)->dynamic_sections_created)
  1480.     {
  1481.       asection *splt;
  1482.       Elf32_External_Dyn *dyncon, *dynconend;
  1483.  
  1484.       splt = bfd_get_section_by_name (dynobj, ".plt");
  1485.       BFD_ASSERT (splt != NULL && sdyn != NULL);
  1486.  
  1487.       dyncon = (Elf32_External_Dyn *) sdyn->contents;
  1488.       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
  1489.       for (; dyncon < dynconend; dyncon++)
  1490.     {
  1491.       Elf_Internal_Dyn dyn;
  1492.       const char *name;
  1493.       asection *s;
  1494.  
  1495.       bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  1496.  
  1497.       switch (dyn.d_tag)
  1498.         {
  1499.         default:
  1500.           break;
  1501.  
  1502.         case DT_PLTGOT:
  1503.           name = ".got";
  1504.           goto get_vma;
  1505.         case DT_JMPREL:
  1506.           name = ".rel.plt";
  1507.         get_vma:
  1508.           s = bfd_get_section_by_name (output_bfd, name);
  1509.           BFD_ASSERT (s != NULL);
  1510.           dyn.d_un.d_ptr = s->vma;
  1511.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  1512.           break;
  1513.  
  1514.         case DT_PLTRELSZ:
  1515.           s = bfd_get_section_by_name (output_bfd, ".rel.plt");
  1516.           BFD_ASSERT (s != NULL);
  1517.           if (s->_cooked_size != 0)
  1518.         dyn.d_un.d_val = s->_cooked_size;
  1519.           else
  1520.         dyn.d_un.d_val = s->_raw_size;
  1521.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  1522.           break;
  1523.  
  1524.         case DT_RELSZ:
  1525.           /* My reading of the SVR4 ABI indicates that the
  1526.          procedure linkage table relocs (DT_JMPREL) should be
  1527.          included in the overall relocs (DT_REL).  This is
  1528.          what Solaris does.  However, UnixWare can not handle
  1529.          that case.  Therefore, we override the DT_RELSZ entry
  1530.          here to make it not include the JMPREL relocs.  Since
  1531.          the linker script arranges for .rel.plt to follow all
  1532.          other relocation sections, we don't have to worry
  1533.          about changing the DT_REL entry.  */
  1534.           s = bfd_get_section_by_name (output_bfd, ".rel.plt");
  1535.           if (s != NULL)
  1536.         {
  1537.           if (s->_cooked_size != 0)
  1538.             dyn.d_un.d_val -= s->_cooked_size;
  1539.           else
  1540.             dyn.d_un.d_val -= s->_raw_size;
  1541.         }
  1542.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  1543.           break;
  1544.         }
  1545.     }
  1546.  
  1547.       /* Fill in the first entry in the procedure linkage table.  */
  1548.       if (splt->_raw_size > 0)
  1549.     {
  1550.       if (info->shared)
  1551.         memcpy (splt->contents, elf_i386_pic_plt0_entry, PLT_ENTRY_SIZE);
  1552.       else
  1553.         {
  1554.           memcpy (splt->contents, elf_i386_plt0_entry, PLT_ENTRY_SIZE);
  1555.           bfd_put_32 (output_bfd,
  1556.               sgot->output_section->vma + sgot->output_offset + 4,
  1557.               splt->contents + 2);
  1558.           bfd_put_32 (output_bfd,
  1559.               sgot->output_section->vma + sgot->output_offset + 8,
  1560.               splt->contents + 8);
  1561.         }
  1562.     }
  1563.  
  1564.       /* UnixWare sets the entsize of .plt to 4, although that doesn't
  1565.      really seem like the right value.  */
  1566.       elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
  1567.     }
  1568.  
  1569.   /* Fill in the first three entries in the global offset table.  */
  1570.   if (sgot->_raw_size > 0)
  1571.     {
  1572.       if (sdyn == NULL)
  1573.     bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
  1574.       else
  1575.     bfd_put_32 (output_bfd,
  1576.             sdyn->output_section->vma + sdyn->output_offset,
  1577.             sgot->contents);
  1578.       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
  1579.       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
  1580.     }
  1581.  
  1582.   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
  1583.  
  1584.   return true;
  1585. }
  1586.  
  1587. #define TARGET_LITTLE_SYM        bfd_elf32_i386_vec
  1588. #define TARGET_LITTLE_NAME        "elf32-i386"
  1589. #define ELF_ARCH            bfd_arch_i386
  1590. #define ELF_MACHINE_CODE        EM_386
  1591. #define elf_info_to_howto        elf_i386_info_to_howto
  1592. #define elf_info_to_howto_rel        elf_i386_info_to_howto_rel
  1593. #define bfd_elf32_bfd_reloc_type_lookup    elf_i386_reloc_type_lookup
  1594. #define ELF_MAXPAGESIZE            0x1000
  1595. #define elf_backend_create_dynamic_sections \
  1596.                     elf_i386_create_dynamic_sections
  1597. #define elf_backend_check_relocs    elf_i386_check_relocs
  1598. #define elf_backend_adjust_dynamic_symbol \
  1599.                     elf_i386_adjust_dynamic_symbol
  1600. #define elf_backend_size_dynamic_sections \
  1601.                     elf_i386_size_dynamic_sections
  1602. #define elf_backend_relocate_section    elf_i386_relocate_section
  1603. #define elf_backend_finish_dynamic_symbol \
  1604.                     elf_i386_finish_dynamic_symbol
  1605. #define elf_backend_finish_dynamic_sections \
  1606.                     elf_i386_finish_dynamic_sections
  1607.  
  1608. #include "elf32-target.h"
  1609.